import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;
import local.util.MyPrint;
import java.text.DecimalFormat;

import local.util.MyStringFormat;
import local.util.MyPrint;

public class YahooDataDownload {    

    private static boolean debug = false;
    private static boolean save = false;
    private String myUrl = null;
    private String urlReturnString = new String();
    private Vector urlReturnVector= new Vector(10);

    public static void main (String[] args) {

	YahooDataDownload  mydl = new YahooDataDownload ();
	String url_return_string = null;
	Vector url_return_vector = new Vector ();
	Vector table_head_v = new Vector ();
	Vector table_record_v_v = new Vector ();

	if (args[0].equals("0"))
	    mydl.setUrl("http://biz.yahoo.com/p/csv/s_conameu.csv"); // Sector
	
	if (args[0].equals("1"))
	    mydl.setUrl("http://biz.yahoo.com/p/utilitconameu.html"); //1
	if (args[0].equals("2"))
	mydl.setUrl("http://biz.yahoo.com/p/transpconameu.html"); //2
	if (args[0].equals("3"))
	mydl.setUrl("http://biz.yahoo.com/p/technoconameu.html"); //3
	if (args[0].equals("4"))
	mydl.setUrl("http://biz.yahoo.com/p/servicconameu.html"); //4
	if (args[0].equals("5"))
	mydl.setUrl("http://biz.yahoo.com/p/healthconameu.html"); //5
	if (args[0].equals("6"))
	mydl.setUrl("http://biz.yahoo.com/p/financconameu.html"); //6
	if (args[0].equals("7"))
	mydl.setUrl("http://biz.yahoo.com/p/energyconameu.html"); //7
	if (args[0].equals("8"))
	mydl.setUrl("http://biz.yahoo.com/p/noncycconameu.html"); //8
	if (args[0].equals("9"))
	mydl.setUrl("http://biz.yahoo.com/p/cyclicconameu.html"); //9
	if (args[0].equals("10"))
	mydl.setUrl("http://biz.yahoo.com/p/congloconameu.html"); //10
	if (args[0].equals("11"))
	mydl.setUrl("http://biz.yahoo.com/p/capgdsconameu.html"); //11
	if (args[0].equals("12"))
	mydl.setUrl("http://biz.yahoo.com/p/basicmconameu.html"); //12
	/*
	mydl.connectToUrl();
	url_return_string = mydl.getUrlReturnString();
	url_return_string = url_return_string.replaceAll("\t", "");
	url_return_string = url_return_string.replaceAll("^@", "");

	//System.out.println(url_return_string);
	url_return_vector = mydl.stringToVector (url_return_string);
	MyPrint.printVector(url_return_vector,10);
	*/
    }

    public void setUrl (String url) {
	myUrl = url;
    }

    public void connectToUrl () {

	String url_return_string = "";

	URL url ;

	HttpsURLConnection httpsConnection = null;

	try {            
	    url = new URL (myUrl);

	    try {              

		StringBuffer buff1= new StringBuffer("");  
		if (myUrl.startsWith("https:")) {
		    httpsConnection = (HttpsURLConnection)url.openConnection();                
		    httpsConnection.setDoOutput(false);                
		    httpsConnection.connect();                

		    if (debug) {
			System.out.println("Connection Code and Message:" + 
					   httpsConnection.getResponseCode() + 
					   " - " + httpsConnection.getResponseMessage()+"\n");
		    }

		    BufferedReader in = new BufferedReader(new InputStreamReader
			(httpsConnection.getInputStream()));

		    url_return_string = in.readLine();

		    while (url_return_string != null) {
			buff1.append(url_return_string);
			url_return_string = in.readLine();
		    }

		    in.close();            
		}
		else if (myUrl.startsWith("http:")) {   

		    HttpURLConnection httpConnection = null;
		    httpConnection = (HttpURLConnection)url.openConnection();                
		    httpConnection.setDoOutput(false);                
		    httpConnection.connect();                

		    if (debug) {
			System.out.println("Connection Code and Message:" + 
					   httpConnection.getResponseCode() + 
					   " - " + httpConnection.getResponseMessage()+"\n");
		    }

		    BufferedReader in = new BufferedReader(new InputStreamReader
			(httpConnection.getInputStream()));

		    url_return_string = in.readLine();
		    
		    while (url_return_string != null) {
			buff1.append(url_return_string+"\n");
			url_return_string = in.readLine();
			//System.out.println(url_return_string);
		    }

		    in.close();            
		}

		url_return_string = buff1.toString();
		
		urlReturnString = url_return_string;

	    } catch (IOException ioe) {                
		System.out.println(ioe.getMessage());  
		System.exit(1);            
	    }        
	} catch (MalformedURLException mue) {            
	     System.out.println(mue.getMessage());            
	    System.exit(1);        
	} catch (Exception e) {            
	    e.printStackTrace();            
	    System.exit(1);        
	}    
    }

    public String getUrlReturnString () {
	return urlReturnString;
    }

    public Vector stringToVector (String input) {

	Vector url_return_vector = new Vector ();
	String element = null;
	StringTokenizer tokens = new StringTokenizer(input, ",");
	while (tokens.hasMoreTokens()) {
	    element = tokens.nextToken();
	    if (element != null) {
		url_return_vector.addElement(element);
	    }
	}

	return url_return_vector;
    }

    private void insertIntoDataBase( String queryStr) {
	int tryTime =0;
	StringBuffer queryBf = new StringBuffer("INSERT INTO sector_record VALUES (");

	queryBf.append(queryStr+");");

	DataBaseInsert myInsert = new DataBaseInsert();	
	
	myInsert.setQueryString(queryBf.toString());
	myInsert.insertData();

    }         
}








